<?php
/*
 * File: RegistrationController.php
 * Project: NewDemo
 * Created Time: 2024-05-30 09:52:24 am
 * Author: WangYuQiang && WangpeiGang (719404171@qq.com)
 * -----
 * Last Modified: 2024-12-04 3:25:27 pm
 * Modified By: WangYuQiang (719404171@qq.com>)
 * -----
 * Copyright (c) 2023 - 2024 成都睿創科技有限公司
 */



namespace App\Http\Controllers\Api;

use App\Models\User;
use Illuminate\Http\Request;
use Tests\CreatesApplication;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\Api\BaseController;

class RegistrationController extends Controller{

    private $Users;
    public function __construct()
    {
        $this->Users = new User();
    }

    /**
     * @param Request $request
     * @return array
     * 用戶註冊
     */
    public function register(Request $request){
        if ($request->has('user_phone') && $request->has('user_password')) {
            $UserPhone = User::where("user_phone", $request->post("user_phone"))->select('user_phone')->first();
            if (!empty($UserPhone)) {
                return $this->error( __('api.phone_exist'));
            }
            $UserEmail = User::where("user_email", $request->post("user_email"))->select('user_email')->first();
            if (!empty($UserEmail)) {
                return $this->error( __('api.email_exist'));
            }
            DB::beginTransaction();
            try {
                $AppName=config('services.basics.base_name');
                $user = new User();
                $user->user_phone = $request->post("user_phone");
                $user->area_code = empty($request->post("area_code")) ? '852' : $request->post("area_code");
                $user->user_password = bcrypt($request->post("user_password"));
                $user->user_email = empty($request->post("user_email")) ?  '' : $request->post("user_email");
                $user->user_name = empty($request->post("user_name")) ?  $AppName.rand(1,3).time() : $request->post("user_name");
                $user->user_number =time().rand(100000,999999);
                $user->user_code =$this->getQRCode($user->user_number);
                $user->user_status = 1;
                $user->login_type = 1;
                $user->user_avatar = config('services.basics.user_avatar');
                $user->created_at = date('Y-m-d H:i:s');
                $user->updated_at = date('Y-m-d H:i:s');
                $user->finally_at = date('Y-m-d H:i:s');
                $res = $user->save();
                if ($res) {
                    $results['token'] = $user->createToken($user->user_id)->accessToken;
                    DB::commit();
                    return $this->success(__('api.registert_success') ,  $results);
                } else {
                    DB::rollBack();
                    return $this->error(__('api.register_fail'));
                }
            } catch (\Exception $e) {
                DB::rollBack();
               return $this->error(__('api.register_fail'),$e->getMessage());
            }
        } else {
            return $this->parametersError(__('api.param_error'));
        }
    }




    

/**********************************************************END**********************************************************************/
}
